var base_url = "/learn/";
// 七牛云url
var qiNiuYun_url = "http://resources.productshow.cn/";

/**
 * 構造JSON字符串
 * 
 * @returns {___anonymous3228_3350}
 */
function createJson() {

	var jsonString = {

		"pageNum" : 1,
		"size" : 20,
		"searchBeans" : {

		},
		"sortBeans" : {

		}
	}

	return jsonString;
}

// 回到顶部
function goToTop(obj) {
	obj.on("tap",function(){
		document.body.scrollTop = 0;
	})
}

goToTop($(".ToTop"));

function post(URL, PARAMS) {
	var temp = document.createElement("form");
	temp.action = URL;
	temp.method = "post";
	temp.style.display = "none";
	for ( var x in PARAMS) {
		var pt = document.createElement("textarea");
		pt.name = x;
		pt.value = PARAMS[x];
		temp.appendChild(pt);
	}
	document.body.appendChild(temp);
	temp.submit();
}

function Get(url , params){
	
	window.location.href = url + "?" + params;
}

(function() {
	var timer
	$(document)[0].onscroll = function() {
		clearTimeout(timer);
		$(".ToTop").show();
		timer = setTimeout(function() {
			$(".ToTop").hide();
		}, 2000)
	}
})();

// 点击阴影
function clickShadow(callback) {
	$(".shadow").on("tap",function() {
		$(this).remove()
		callback && callback();
	})
}
// 提示弹框
function Tip() {
	this.param = {
		header : "",
		content : "tips",
		bottom : "",
		skin : "skin",
		showWay : 'fade'
	};
}
Tip.prototype = {
	// 弹框的内容
	renderUI : function(p) {
		$.extend(this.param, p)
		this.Box = $('<div class="tips ' + this.param.skin + '">'
				+ '<div class=tips_header>' + this.param.header + '</div>'
				+ '<div class=tips_content>' + this.param.content + '</div>'
				+ '<div class=tips_bottom>' + this.param.bottom + '</div>'
				+ '</div>')
		this.Box.appendTo("body");
		return this
	},
	// 显示弹框的方法
	show : function() {
		var _this = this;
		if (this.param.showWay == 'fade' && this.Box) {
			clearTimeout(this.timer)
			this.Box.css({
				opacity : 1,
				visibility : "visible"
			})
			this.timer = setTimeout(function() {
				_this.Box.css({
					opacity : 0,
					visibility : "hidden"
				})
			}, 1500)
		}else{
			this.Box.css({
				opacity:1,
				visibility:"visible"
			})
		}
		return this;
	},
	// 重写弹框的内容
	content : function(txt) {
		this.Box && this.Box.find(".tips_content").text(txt)
		return this;
	}
}
var tip = new Tip();
tip.renderUI();

/**
 * 
 * @param method
 *            请求方式
 * @param url
 *            请求路径
 * @param params
 *            请求数据对象
 * @param successCallback
 *            成功后的回调方法
 * @param errorCallback
 *            错误后的回调方法
 * @param isAsync
 *            是否异步
 * @param isFile
 *            是否含文件上传
 */
function doAjax(method, url, params, successCallback, errorCallback, isAsync,
		isFile) {
	if (method === "POST") {
		if (isFile === true) {
			$.ajax({
				type : "POST",
				url : url,
				data : params,
				dataType : "json",
				async : isAsync,
				cache : false,
				processData : false,
				contentType : false,
				success : successCallback,
				error : errorCallback
			});
		} else {
			$.ajax({
				type : "POST",
				url : url,
				data : params,
				async : isAsync,
				dataType : "json",
				success : successCallback,
				error : errorCallback
			});
		}
	} else if (method === "GET") {
		$.ajax({
			type : "GET",
			url : url,
			data : params,
			async : isAsync,
			dataType : "json",
			success : successCallback,
			error : errorCallback
		});
	} else {
		return;
	}
}

function formatTime(obj, type) {
	console.log(obj)

	// 获取日期
	var date = new Date();
	date.setTime(obj);
	var str = "";
	// 获取月
	var month = checkTime((date.getMonth() + 1) + "");
	var day = checkTime(date.getDate() + "");
	var hour = checkTime(date.getHours() + "");
	var minute = checkTime(date.getMinutes() + "");
	var second = checkTime(date.getSeconds() + "");
	

	// 进行类型转换
	switch (type) {

	case 0:
		str = date.getFullYear() + "-" + month + "-" + day + " "
				+ hour + ":" + minute + ":" + second;
		break;
	case 1:
		str = date.getFullYear() + "/" + month + "/" + day + " "
				+ hour + ":" + minute + ":" + second;
		break;

	default:
		break;
	}

	return str;
}

/**
 * 检验时间是否小于0
 * 
 * @param time
 */
function checkTime(time) {
	if (Number(time) < 10) {
		time = 0 + time;
	}

	return time;
}

//大的加载中
window.onload=function(){
	$(".loading_container").remove();
	lazyLoad($(".lazy"))
}

//图片延迟加载,有坑
function lazyLoad(obj){
		var winH = $(window).height();
		var scrollH = $(window).scrollTop();
		obj.each(function(){
			var _this = $(this)
			var src = _this.attr("data-src")
			var top = _this.offset().top;
			var load = _this.attr("data-load")
			if(top<=winH+scrollH&&_this.attr("data-load")=="false"){
				console.log("我加载了图片");
				var imgObj = new Image();
				imgObj.onload=function(){
					_this.attr("src",src).attr("data-load","true")
				}
				imgObj.src = src
			}
		})
}
//后退
$(".back").on("tap",function(){
//	console.log("1");
	history.go(-1);
})
//图片格式化,拉伸
var FormatImg = function(selected,container){
			selected.each(function(){
				var imgsrc =  $(this).attr("data-src")||$(this).attr("src");
				var wImage = $(this);
				var imgobj = new Image();
				imgobj.onload = function(){
				var h = this.height;
				var w = this.width;
				var winWidth = container.width();
				var winHeight = container.height();
				var rh = winWidth*h/w;
				var rw = winHeight*w/h;
				var paddingLeft = parseInt((winWidth-rw)/2);
				var paddingTop = parseInt((winHeight-rh)/2);
				if(h/w>winHeight/winWidth)	{
					wImage.attr("src",imgsrc).css("height",winHeight).css("padding-left",paddingLeft);
				}else{
					wImage.attr("src",imgsrc).css("width","100%").css("padding-top",paddingTop);
				}
			}
			imgobj.src = imgsrc;
				
			})
		}
